/*
 * @Description: mock请求
 * @Author: xinqiao lxq1990.0716@qq.com
 * @Date: 2025-03-20
 * @LastEditors: xinqiao lxq1990.0716@qq.com
 * @LastEditTime: 2025-03-27
 */

import aiMock from '@ssc-plugin/mock'

import { isLocation, isMock } from '/@/utils'
import { isArray } from '/@/utils/is'
import apiDatas from '/mock/output/api'
import modelDatas from '/mock/output/model'

/** mock列表总条数 */
const MOCK_LIST_TOTAL = 100

/** mock列表每页条数 */
const MOCK_LIST_PAGESIZE = 3

/**
 * 校验是否需要mock
 * @param url 请求链接
 * @returns
 */
export const needMock = (url: string) => {
  if (!isLocation()) return false
  if (!isMock(url)) return false
  return true
}

/**
 * mock数据
 * @param url 请求链接
 * @returns
 */
export const mock = async (url: string) => {
  aiMock.init({ apiDatas, modelDatas, listTotal: MOCK_LIST_TOTAL, listPageSize: MOCK_LIST_PAGESIZE })
  const mockRes = await aiMock.requestMock(url)
  const result = { responseCode: 100000000, responseMessage: '操作成功', responseData: {} }
  if (isArray(mockRes)) {
    result.responseData = { total: MOCK_LIST_TOTAL, list: mockRes }
  } else {
    result.responseData = mockRes as Object
  }
  return result
}
